var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store
Page({

  /**
   * 页面的初始数据
   */
  data: {
    depart:'',
    departId:'',
    supervisorName:'',
    supervisorId:'',
    operatorName:'',
    operatId:'',
    dutyLeaderId:'',
    dutyLeaderName:'',
    operateContent:'',
    riskList:[],
    riskMeasures:[
      {
        id:0,
        operation_order:'',
        operationRiskPoint:'',
        list:['']
      }
    ],
    ticketInfo:{},
    info:{
      submit:0
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      ticketNumber:options.ticketNumber,
      
    })
    api.$get(api.operateTicket.operateTickInfo+options.ticketNumber).then((res) => {
      this.setData({
        ticketInfo:res.data,
        supervisorName:res.data.acceptorName,
        supervisorId:res.data.acceptorId,
        dutyLeaderName:res.data.ticketIssuerName,
        dutyLeaderId:res.data.ticketIssuerId
      })
      if(res.data.operationRiskControlCard.txt)this.setData({
        riskMeasures:JSON.parse(res.data.operationRiskControlCard.txt)
      })
    })
    api.$get(api.operateTicket.operationRiskList).then((res) => {
      this.setData({
        riskList:res.data,
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var form = Store.getState().formInfor.form
    var key = form.key
    if (!form.selected) return
    switch (key) {
      case 'selectDuty': // 获取值班人  
        this.setData({
          dutyLeaderName: form.selected.userName,
          dutyLeaderId: form.selected.userId,
        })
        break
      case 'selectSupervisor': // 获取监护人  
        this.setData({
          supervisorName: form.selected.userName,
          supervisorId: form.selected.userId,
        })
        break
    }
    console.log(form)
  },
  selectDuty() {
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectDuty', // 后台要的更改字段 
        type: 1, // 表单类型 1： radio  2：select
        config: {
          // deptId:this.data.ticketInfo.deptId
          deptId:0
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)  
  },
  selectSupervisor(){
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectSupervisor', // 后台要的更改字段 
        type: 1, // 表单类型 1： radio  2：select
        config: {
          // deptId:this.data.ticketInfo.deptId
          deptId:0
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  openOrder(e){
    var that=this
    const {key,index}=$.detail(e)
    wx.showModal({
      title: '操作顺序',
      editable: true,
      placeholderText:'请输入顺序范围,例2~10',
      success (res) {
        if (res.confirm) {
          that.setData({
            [`riskMeasures[${index}].${key}`]:res.content
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  openPerilPoint(e){
    const index=e.target.dataset.index
    this.data.riskMeasures[index].operationRiskPoint=this.data.riskList[e.detail.value] 
    this.setData({
      riskMeasures:this.data.riskMeasures 
    })
    api.$get(api.operateTicket.riskControlLibrary,{ticketNumber:this.data.ticketNumber,operationRiskPoint:this.data.riskMeasures[index].operationRiskPoint}).then((res) => {
      const measures=res.data.map((item)=>item.controlMeasures)
      this.data.riskMeasures[index].list=[{...res.data[0], controlMeasures: measures.join(';')}]
      this.setData({
        riskMeasures:this.data.riskMeasures
      })
    })
  },
  changePreControl(e){
    const key=e.currentTarget.dataset.key
    const index=e.currentTarget.dataset.index
    this.setData({
      [`riskMeasures[${index}].list[${0}].${key}`]:e.detail.value
    })
  },
  addMeasure(){
    var risks=this.data.riskMeasures
    risks.push({
      id:risks.length,
      operation_order:'',
      operationRiskPoint:'',
      list:['']
    })
    this.setData({riskMeasures:risks})
  },
  cancelBack(){ 
    $.linkurl(`/pages/operationTicket/operationIndex/operationIndex`,'back')
  },
  subRiskCard(){
    api.$post(api.operateTicket.riskControlTasks,{ticketNumber:this.data.ticketNumber,riskPointMeasureList:this.data.riskMeasures}).then((res) => {
      api.$post(api.operateTicket.addRiskControlTasks,{...res.data,supervisorId:this.data.supervisorId,dutyLeaderId:this.data.dutyLeaderId}).then((res2) => {
        api.$get(api.operateTicket.submitTicketWork,{ticketNumber:this.data.ticketNumber}).then((res3) => {
          $.linkurl(`/pages/operationTicket/operationIndex/operationIndex`,'back')
        })
      })
    })
    
  }
})